﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;

using XmlVisualizer.Controls;
using XmlVisualizer.Models;

namespace XmlVisualizer.Dialogs {
	/// <summary>
	/// Interaction logic for DialogAddNewElement.xaml
	/// </summary>
	public partial class DialogAddNewElement : Window {
		#region Fields
		bool posAdjusted = false;
		XNodeInfo newElement = null;
		#endregion

		#region Dependency properties
		/// <summary>
		/// Identifies the ElementProperty.
		/// </summary>
		public static DependencyProperty AttributesProperty;

		/// <summary>
		/// Gets or sets the ElemenProperty.
		/// </summary>
		public AttributesList Attributes {
			get { return (AttributesList)GetValue(AttributesProperty); }
			set { SetValue(AttributesProperty, value); }

		}
		#endregion

		#region Constructors
		/// <summary>
		/// Initializes the DialogAddNewElement class.
		/// </summary>
		static DialogAddNewElement() {
			FrameworkPropertyMetadata attributesMetadata = new FrameworkPropertyMetadata(new AttributesList(),
				FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
			AttributesProperty = DependencyProperty.Register("Attributes", typeof(AttributesList), typeof(DialogAddNewElement), attributesMetadata);
		}

		/// <summary>
		/// Initializes a new instance of the DialogAddNewElement class.
		/// </summary>
		public DialogAddNewElement() {
			InitializeComponent();
			Attributes = new AttributesList();
		}
		#endregion

		#region Methods
		/// <summary>
		/// Displays and shows the window until user closes it.
		/// </summary>
		/// <returns><see cref="DialogAddElementAttribute.IDOK">IDOK</see> on successful closure,
		/// <see cref="DialogAddElementAttribute.IDCANCEL">IDCANCEL</see> otherwise.</returns>
		public XNodeInfo ShowModal() {
			ShowDialog(); // this "blocks" until window is closed
			return newElement;
		}
		#endregion

		#region Event handlers
		/// <summary>
		/// Invoked when window gets activated.
		/// </summary>
		/// <param name="e">The event data.</param>
		protected override void OnActivated(EventArgs e) {
			base.OnActivated(e);
			if (!posAdjusted) {
				this.Left -= this.ActualWidth;
				this.Top -= this.ActualHeight;
				posAdjusted = true;
			}

			tb_ElementName.Focus();
			tb_ElementName.SelectAll();
		}

		/// <summary>
		/// Invoked when user clicks on the button.
		/// </summary>
		/// <param name="sender">The object where the event handler is attached.</param>
		/// <param name="e">The event data.</param>
		private void Button_Click(object sender, RoutedEventArgs e) {
			String namespacePrefix = tb_NamespacePrefix.Text;
			XNamespace ns = tb_ElementNamespace.Text;
			XName name = ns.GetName(tb_ElementName.Text);
			
			XElement element = new XElement(name);

			if (!String.IsNullOrEmpty(namespacePrefix)) {
				element.SetAttributeValue(XNamespace.Xmlns + namespacePrefix, ns.NamespaceName);
			}
			
			foreach (XAttribute att in Attributes) {
				element.SetAttributeValue(att.Name, att.Value);
			}
			//newElement = element;
			if (border_Appearances.Child is ComboBox) {
				ComboBox cb = border_Appearances.Child as ComboBox;
				object selectedItem = cb.SelectedItem;
				if (selectedItem is ComboBoxItem) {
					ComboBoxItem selectedComboBoxItem = selectedItem as ComboBoxItem;
					string appearance = selectedComboBoxItem.Tag as String;
					XNodeInfo info = new XNodeInfo(new Point(0, 0), 0, 0, appearance, element);
					newElement = info;
				}
			}
			Close();
		}
		#endregion		
	}
}
